﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using DBUtility;
using MySql.Data.MySqlClient;

namespace DAL
{
    public class DSpecialCar
    {
        public bool PhoneNumberExists(string phoneNumber)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tSpecialCar");
            strSql.Append(" where PhoneNumber=@PhoneNumber");
            MySqlParameter[] parameters = {
					new MySqlParameter("@PhoneNumber", MySqlDbType.VarChar,12)
          };
            parameters[0].Value = phoneNumber;
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
            {
                return false;
            }
            int result = int.Parse(ds.Tables[0].Rows[0][0].ToString());
            return result > 0;
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.SpecialCarInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tSpecialCar(");
            strSql.Append("Type,PhoneNumber,IMEI,DriverName,");
            strSql.Append("CarNo,CarType,CompanyId,ServerId,Signature)");
            strSql.Append(" values (");
            strSql.Append("@Type,@PhoneNumber,@IMEI,@DriverName,");
            strSql.Append("@CarNo,@CarType,@CompanyId,@ServerId,@Signature)");             
            MySqlParameter[] parameters = {
					new MySqlParameter("@Type", MySqlDbType.Int32),
                    new MySqlParameter("@PhoneNumber", MySqlDbType.VarChar,12),
                    new MySqlParameter("@IMEI", MySqlDbType.VarChar,20),
                    new MySqlParameter("@DriverName", MySqlDbType.VarChar,10),
                    new MySqlParameter("@CarNo", MySqlDbType.VarChar,8),
                    new MySqlParameter("@CarType", MySqlDbType.VarChar,15),
                    new MySqlParameter("@CompanyId", MySqlDbType.Int32),
                    new MySqlParameter("@ServerId", MySqlDbType.Int32),
                    new MySqlParameter("@Signature", MySqlDbType.Int32)
                                          };
            parameters[0].Value = model.Type;
            parameters[1].Value = model.PhoneNumber;
            parameters[2].Value = model.Imei;
            parameters[3].Value = model.DriverName;
            parameters[4].Value = model.CarNo;
            parameters[5].Value = model.CarType;
            parameters[6].Value = model.CompanyId;
            parameters[7].Value = model.ServerId;
            parameters[8].Value = model.Signature;
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool Update(Model.SpecialCarInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tSpecialCar set ");
            strSql.Append("Type=@Type,");
            strSql.Append("PhoneNumber=@PhoneNumber,");
            strSql.Append("IMEI=@IMEI,");
            strSql.Append("DriverName=@DriverName,");
            strSql.Append("CarNo=@CarNo,");
            strSql.Append("CarType=@CarType,");
            strSql.Append("CompanyId=@CompanyId,");
            strSql.Append("ServerId=@ServerId,");
            strSql.Append("Signature=@Signature");

            strSql.Append(" where Id=@id");
            
            MySqlParameter[] parameters = {
					new MySqlParameter("@Type", MySqlDbType.Int32),
                    new MySqlParameter("@PhoneNumber", MySqlDbType.VarChar,12),
                    new MySqlParameter("@IMEI", MySqlDbType.VarChar,20),
                    new MySqlParameter("@DriverName", MySqlDbType.VarChar,10),
                    new MySqlParameter("@CarNo", MySqlDbType.VarChar,8),
                    new MySqlParameter("@CarType", MySqlDbType.VarChar,15),
                    new MySqlParameter("@CompanyId", MySqlDbType.Int32),
                    new MySqlParameter("@ServerId", MySqlDbType.Int32),
                    new MySqlParameter("@Signature", MySqlDbType.Int32),
                    new MySqlParameter("@id", MySqlDbType.Int32,10)};
            parameters[0].Value = model.Type;
            parameters[1].Value = model.PhoneNumber;
            parameters[2].Value = model.Imei;
            parameters[3].Value = model.DriverName;
            parameters[4].Value = model.CarNo;
            parameters[5].Value = model.CarType;
            parameters[6].Value = model.CompanyId;
            parameters[7].Value = model.ServerId;
            parameters[8].Value = model.Signature;
            parameters[9].Value = model.Id;

           
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool UpdateImeiByPhoneNumber(string phoneNumber,string imei)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tSpecialCar set ");            
            strSql.Append("IMEI=@IMEI ");             
            strSql.Append(" where PhoneNumber=@PhoneNumber");             
            MySqlParameter[] parameters = {
					new MySqlParameter("@IMEI", MySqlDbType.VarChar),
                    new MySqlParameter("@PhoneNumber", MySqlDbType.VarChar,12)
                    };
            parameters[0].Value = imei;
            parameters[1].Value = phoneNumber;
             
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }



        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tSpecialCar ");
            strSql.Append(" where Id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)};
            parameters[0].Value = id;
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.SpecialCarInfo GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from tSpecialCar ");
            strSql.Append(" where Id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)
};
            parameters[0].Value = id;

            Model.SpecialCarInfo model = new Model.SpecialCarInfo();
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Type"] != null && ds.Tables[0].Rows[0]["Type"].ToString() != "")
                {
                    model.Type = int.Parse(ds.Tables[0].Rows[0]["Type"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PhoneNumber"] != null && ds.Tables[0].Rows[0]["PhoneNumber"].ToString() != "")
                {
                    model.PhoneNumber = ds.Tables[0].Rows[0]["PhoneNumber"].ToString();
                }
                if (ds.Tables[0].Rows[0]["DriverName"] != null && ds.Tables[0].Rows[0]["DriverName"].ToString() != "")
                {
                    model.DriverName = ds.Tables[0].Rows[0]["DriverName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["CarNo"] != null && ds.Tables[0].Rows[0]["CarNo"].ToString() != "")
                {
                    model.CarNo = ds.Tables[0].Rows[0]["CarNo"].ToString();
                }
                if (ds.Tables[0].Rows[0]["CarType"] != null && ds.Tables[0].Rows[0]["CarType"].ToString() != "")
                {
                    model.CarType = ds.Tables[0].Rows[0]["CarType"].ToString();
                }               
                if (ds.Tables[0].Rows[0]["CompanyId"] != null && ds.Tables[0].Rows[0]["CompanyId"].ToString() != "")
                {
                    model.CompanyId = int.Parse(ds.Tables[0].Rows[0]["CompanyId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ServerId"] != null && ds.Tables[0].Rows[0]["ServerId"].ToString() != "")
                {
                    model.ServerId = int.Parse(ds.Tables[0].Rows[0]["ServerId"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.SpecialCarInfo GetModelByPhoneNum(string phoneNumber)
        {
             
            StringBuilder strSql = new StringBuilder(); 
            strSql.Append("select t.Id,t.Type,t.PhoneNumber,t.IMEI,");
            strSql.Append("t.DriverName,t.CarNo,t.CarType,t.CompanyId,");
            strSql.Append("t.ServerId,tcompany.CompanyName,");
            strSql.Append("tserverconfig.ServerName");
            strSql.Append(" FROM tspecialcar t ");
            strSql.Append("left join tcompany on t.CompanyId = ");
            strSql.Append("tcompany.Id ");
            strSql.Append("left join tserverconfig on t.ServerId= ");
            strSql.Append("tserverconfig.id");
            strSql.Append(" where t.PhoneNumber=@PhoneNumber");
            MySqlParameter[] parameters = {
					new MySqlParameter("@PhoneNumber", MySqlDbType.VarChar)
};
            parameters[0].Value = phoneNumber;

            Model.SpecialCarInfo model = new Model.SpecialCarInfo();
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Type"] != null && ds.Tables[0].Rows[0]["Type"].ToString() != "")
                {
                    model.Type = int.Parse(ds.Tables[0].Rows[0]["Type"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PhoneNumber"] != null && ds.Tables[0].Rows[0]["PhoneNumber"].ToString() != "")
                {
                    model.PhoneNumber = ds.Tables[0].Rows[0]["PhoneNumber"].ToString();
                }
                if (ds.Tables[0].Rows[0]["DriverName"] != null && ds.Tables[0].Rows[0]["DriverName"].ToString() != "")
                {
                    model.DriverName = ds.Tables[0].Rows[0]["DriverName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["CarNo"] != null && ds.Tables[0].Rows[0]["CarNo"].ToString() != "")
                {
                    model.CarNo = ds.Tables[0].Rows[0]["CarNo"].ToString();
                }
                if (ds.Tables[0].Rows[0]["CarType"] != null && ds.Tables[0].Rows[0]["CarType"].ToString() != "")
                {
                    model.CarType = ds.Tables[0].Rows[0]["CarType"].ToString();
                }
                if (ds.Tables[0].Rows[0]["CompanyId"] != null && ds.Tables[0].Rows[0]["CompanyId"].ToString() != "")
                {
                    model.CompanyId = int.Parse(ds.Tables[0].Rows[0]["CompanyId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ServerId"] != null && ds.Tables[0].Rows[0]["ServerId"].ToString() != "")
                {
                    model.ServerId = int.Parse(ds.Tables[0].Rows[0]["ServerId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CompanyName"] != null && ds.Tables[0].Rows[0]["CompanyName"].ToString() != "")
                {
                    model.CompanyName = ds.Tables[0].Rows[0]["CompanyName"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select t.Id,t.Type,t.PhoneNumber,t.IMEI,");
            strSql.Append("t.DriverName,t.CarNo,t.CarType,t.CompanyId,");
            strSql.Append("t.ServerId,tcompany.CompanyName,");
            strSql.Append("tserverconfig.ServerName");
            strSql.Append(" FROM tspecialcar t ");
            strSql.Append("left join tcompany on t.CompanyId = ");
            strSql.Append("tcompany.Id ");
            strSql.Append("left join tserverconfig on t.ServerId= ");
            strSql.Append("tserverconfig.id");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperMySQL.Query(strSql.ToString());
        }
    }
}
